import { _decorator, Component, find, instantiate,screen, Node, Prefab,tween } from 'cc';
const { ccclass, property } = _decorator;
import { Enemy } from './Enemy';
import { boss } from './boss';
import GlobalData from './GlobalData';
@ccclass('createEnemy')
export class createEnemy extends Component {

    // 敌人预制体
    @property(Prefab)
    enemyPre:Prefab = null;
    // 敌人预制体挂载节点
    @property(Node)
    enemyNode:Node = null;
    // buff节点
    @property(Node)
    buffNode:Node = null;
    // boss节点
    @property(Node)
    bossNode:Node = null;

    // 四傻预制体
    // 敌人预制体挂载节点
    @property(Prefab)
    jiulingNode:Prefab = null;
    // 敌人预制体挂载节点
    @property(Prefab)
    ckNode:Prefab = null;
    // 敌人预制体挂载节点
    @property(Prefab)
    laojianNode:Prefab = null;
    // 敌人预制体挂载节点
    @property(Prefab)
    zhuboNode:Prefab = null;

    // 玩家节点
    private playerNode :Node = null;

    // 四个敌人生成定时器
    public leftTimer = null;
    public rightTimer = null;
    public topTimer = null;
    public bottomTimer = null;

    // 敌人生成时间间隔
    private createTime = 2000
    // 生成敌人当前的血量
    private enemyHealth = 100
    public kangxing = 1
    // 检测敌人血量是否超过x百
    private bai = 1
    // 生成boss次数
    private bossNums = 1
    // boss是否已经生成
    public isBoss:boolean = false
    // 是否开启了无尽模式
    public endless:boolean = false

    // boss血量
    public bossHealth = 10000

    onLoad() {
        // 获取全局数据实例
        const globalData = GlobalData.getInstance();
        
        // 读取传递的参数并使用
        console.log("玩家等级：", globalData.playerLevel);
        console.log("当前关卡：", globalData.checkPoint);
        if(globalData.checkPoint == 0){
            this.endless = true
        }
    }
    
    start() {
        this.startCreate()
    }
    // 所有创建敌人定时器同一注册开始生成敌人
    startCreate(){
        if(this.isBoss)return// 生成boss后不会继续生成敌人
        // 获取全局数据实例
        const globalData = GlobalData.getInstance();

        this.playerNode = find('Canvas/me')
        let windowSize = screen.windowSize;
        let nowWidth = windowSize.width / 2;
        let nowHeight = windowSize.height / 2;
        if(this.endless){// 无尽模式下小怪增强效果强
            this.enemyHealth *= 1.1
        }else{
            this.enemyHealth += (30 * globalData.checkPoint)// 敌人每次血量增加10*关卡数
        }
        this.createBoss()
        // 设置新定时器前清除旧定时器
        this.stopCreate()
        if(Math.floor(this.enemyHealth / 100) > this.bai){
            // console.log(Math.floor(this.enemyHealth / 100),this.bai)
            this.bai += 2// 关乎敌人抗性加强和生成间隔减少的问题
            if(this.kangxing-0.1 >= 0.1){
                this.kangxing -= 0.1
                this.kangxing.toFixed(1)
            }
            if(this.createTime > 1500){
                this.createTime -= 100
            }
            // console.log("敌人抗性:"+this.kangxing)
            // console.log("生成敌人时间间隔:"+this.createTime)
        }
        // 根据左上右下四个方位随机生成敌人
        this.leftTimer = setInterval(()=>{//左
            let enemy = instantiate(this.jiulingNode)
            // 设置敌人血量
            enemy.getComponent(Enemy).health = this.enemyHealth
            enemy.getComponent(Enemy).kangxing = this.kangxing
            // 随机位置
            let randomNum = Math.random()*nowHeight
            enemy.setParent(this.enemyNode)
            let playerPos = this.playerNode.position.clone()
            enemy.setPosition(playerPos.x-1000,playerPos.y+randomNum,0)
            // console.log("生成敌人")
        },this.createTime)
        this.topTimer = setInterval(()=>{//上
            let enemy = instantiate(this.ckNode)
            // 设置敌人血量
            enemy.getComponent(Enemy).health = this.enemyHealth
            enemy.getComponent(Enemy).kangxing = this.kangxing
            // 随机位置
            let randomNum = Math.random()*nowWidth
            enemy.setParent(this.enemyNode)
            let playerPos = this.playerNode.position.clone()
            enemy.setPosition(playerPos.x+randomNum,playerPos.y+1000)
        },this.createTime)
        this.rightTimer = setInterval(()=>{//右
            let enemy = instantiate(this.zhuboNode)
            // 设置敌人血量
            enemy.getComponent(Enemy).health = this.enemyHealth
            enemy.getComponent(Enemy).kangxing = this.kangxing
            // 随机位置
            let randomNum = Math.random()*nowHeight
            enemy.setParent(this.enemyNode)
            let playerPos = this.playerNode.position.clone()
            enemy.setPosition(playerPos.x+1000,playerPos.y+randomNum)
        },this.createTime)
        this.bottomTimer = setInterval(()=>{//下
            let enemy = instantiate(this.laojianNode)
            // 设置敌人血量
            enemy.getComponent(Enemy).health = this.enemyHealth
            enemy.getComponent(Enemy).kangxing = this.kangxing
            // 随机位置
            let randomNum = Math.random()*nowWidth
            enemy.setParent(this.enemyNode)
            let playerPos = this.playerNode.position.clone()
            enemy.setPosition(playerPos.x+randomNum,playerPos.y-1000)
        },this.createTime)
    }
    // 生成boss
    createBoss(){
        if(Math.floor(this.enemyHealth / 1000) > this.bossNums){// 当普通敌人生命超过1000时生成boss
        // if(this.enemyHealth > 110){// 测试用的检测生成boss
            // boss已生成
            this.bossNums *= 10
            // console.log("boss已生成")
            // 生成boss时停止生成敌人
            this.stopCreate()
            if(this.isBoss)return
            let newBoss = instantiate(this.bossNode)
            let windowSize = screen.windowSize;
            let nowWidth = windowSize.width / 2;
            let nowHeight = windowSize.height / 2;
            // 设置敌人血量
            // 设置敌人血量
            newBoss.getComponent(boss).health = this.enemyHealth * 10
            console.log("boss已生成,血量为:",this.enemyHealth * 10)
            // newBoss.getComponent(boss)
            newBoss.active = true
            // 随机位置
            let randomNum = Math.random()*nowHeight
            newBoss.setParent(this.enemyNode)
            let playerPos = this.playerNode.position.clone()
            newBoss.setPosition(playerPos.x-1000,playerPos.y+randomNum,0)
            this.isBoss = true
        }
    }
    // 所有定时器同一注销
    stopCreate(){
        // if(this.endless)return// 无尽模式不会关闭敌人生成
        clearInterval(this.leftTimer)
        clearInterval(this.topTimer)
        clearInterval(this.rightTimer)
        clearInterval(this.bottomTimer)
    }
}


